package me.sealer.ssm.controller;

import lombok.extern.slf4j.Slf4j;
import me.sealer.ssm.model.ShortUrl;
import me.sealer.ssm.service.ShortUrlService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;

/**
 * 短网址服务
 *
 * @author sealer
 * @date 2017/06/19
 */
@Slf4j
@Controller
public class DwzController {
    private final ShortUrlService shortUrlService;

    public DwzController(ShortUrlService shortUrlService) {
        this.shortUrlService = shortUrlService;
    }

    @RequestMapping(value = "/short/{sUrl}")
    public void redirect(HttpServletResponse response, @PathVariable String sUrl) {
        ShortUrl shortUrl = shortUrlService.selectByShortUrl(sUrl);
        response.setHeader("Location", shortUrl.getLongUrl());
        response.setStatus(302);
    }
}
